package com.information.manage.basic.common.utils.exportAndImport.custom;

import com.information.manage.basic.common.utils.exportAndImport.constant.ColorEnum;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义导出是数据样式
 */
@Component
public class CustomExportExcelStyle implements CommandLineRunner {

    @Resource
    private ExImportManager exImportManager;

    private List<CustomExportExcelStyleOperator> list = new ArrayList<>();
    {
        //添加不同的样式实现时 添加管理
        list.add(new defaultCustomExport());
        list.add(new phaseStyleSeparationCustomExport());
    }

    @Override
    public void run(String... args) {
        exImportManager.addStyleOperator(list);
    }

    /**
     * 默认数据不添加任何样式
     */
    public class defaultCustomExport implements CustomExportExcelStyleOperator {
        @Override
        public void functionExcelStyle(List<?> list, XSSFSheet sheet, XSSFWorkbook workbook, XSSFRow row, Integer index) {
            if (list != null) {  // 遍历数据
                for (Object o : list) {
                    Class aClass = o.getClass();
                    row = sheet.createRow(index);
                    int cellIndex = 0;
                    // 获取数据字段名称
                    Field[] declaredFields = aClass.getDeclaredFields();
                    for (Field declaredField : declaredFields) {
                        //设置是否允许访问，不是修改原来的访问权限修饰词。
                        declaredField.setAccessible(true);
                        XSSFCell cell = row.createCell((short) cellIndex);
                        // 设置表格数据
                        try {
                            if (declaredField.get(o) != null && !"".equals(declaredField.get(o))){
                                cell.setCellValue(declaredField.get(o).toString());
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        cellIndex++;
                    }
                    index++;
                }
            }
        }
    }


    public class phaseStyleSeparationCustomExport implements CustomExportExcelStyleOperator {
        @Override
        public void functionExcelStyle(List<?> list, XSSFSheet sheet, XSSFWorkbook workbook, XSSFRow row, Integer index) {
            try {
                if (list != null) {  // 遍历数据
                    for (Object o : list) {
                        Class aClass = o.getClass();
                        row = sheet.createRow(index);
                        int cellIndex = 0;
                        // 获取数据字段名称
                        Field[] declaredFields = aClass.getDeclaredFields();
                        for (Field declaredField : declaredFields) {
                            //设置是否允许访问，不是修改原来的访问权限修饰词。
                            declaredField.setAccessible(true);
                            XSSFCell cell = row.createCell((short) cellIndex);
                            //当字段值存在且满足一定条件时设置颜色样式
//                            if (declaredField.equals("total")){  // 满足某个字段时填充其颜色显示
//                                Field color = aClass.getField("color");
//                                if (color != null){
//                                    Short colorIndex = Short.valueOf(color.get(o).toString());
//                                    // 生成一个样式
//                                    XSSFCellStyle dataStyle = workbook.createCellStyle();
//                                    // 设置颜色
//                                    dataStyle.setFillForegroundColor(colorIndex);
//                                    dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//                                    cell.setCellStyle(dataStyle);
//                                }
//                            }
                            if (declaredField.getName().equals("exceed")){
                                if (Integer.valueOf(declaredField.get(o).toString()) > 0){
                                    // 生成一个样式
                                    XSSFCellStyle dataStyle = workbook.createCellStyle();
                                    // 设置颜色
                                    dataStyle.setFillForegroundColor(ColorEnum.YELLOW.index);
                                    dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                                    cell.setCellStyle(dataStyle);
                                }
                            }
                            // 设置表格数据
                            if (declaredField.get(o) != null && !"".equals(declaredField.get(o))){
                                cell.setCellValue(declaredField.get(o).toString());
                            }
                            cellIndex++;
                        }
                        index++;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

}
